/******************************************************************************
 * Copyright (C) 2016  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_SYSTEM_THREAD_POSIX_H_
#define NODE_SYSTEM_THREAD_POSIX_H_

#include <unistd.h>
#include <pthread.h>
#include <node/noncopyable.h>

namespace node {
	namespace system {
	class runnable;

	class thread_impl : private noncopyable
	{
	public:
		enum priority
        {
            PRIO_LOWEST_IMPL = 0,
            PRIO_LOW_IMPL = 1,
            PRIO_NORMAL_IMPL = 2,
            PRIO_HIGH_IMPL = 3,
            PRIO_HIGHEST_IMPL = 4
        };
        typedef pthread_t   id;

        thread_impl(void);
        ~thread_impl(void);

        bool start_impl(runnable* runnable);
        void join_impl(void);
        bool joinable_impl(void);
        bool set_priority_impl(int priority);
        id id_impl(void) const { return _id; }

    private:
        static void* proc(void*);

        id _id;
        runnable* _runnable;
	};

	class this_thread_impl : private noncopyable
	{
	public:
		static thread_impl::id id_impl(void) {
			return pthread_self();
		}
		static void sleep_impl(long milliseconds) {
			usleep(int(milliseconds * 1000));
		}
	};
}}

#endif // NODE_SYSTEM_THREAD_POSIX_H_